

PM_FUNC_NORM_ATTRIB LoadPlayer( Int:iPlayer )
{
	pokemod_funcstart_debug( "LoadPlayer" );

	new String:szPlayerFile[ 64 ];
	nfv_player_file( iPlayer, szPlayerFile, charsmax( szPlayerFile ) );

	new String:szVersion[ 12 ];
	if ( !nfv_get_data( szPlayerFile, "PokeMod", "Version", szVersion, charsmax( szVersion ) ) )
	{
		for ( new Int:iPokeNum, Int:iSkillNum, PM_POKESTAT:iStatNum;
			iPokeNum < POKEMON_PER_PLAYER;
			iPokeNum++
		){
			for ( iStatNum = PM_POKESTAT:0; iStatNum < PM_POKESTAT; iStatNum++ )
				g_iPlayerPokemonStats[ iPlayer ][ iPokeNum ][ iStatNum ] = 0;

			g_szPlayerPokemonNames[ iPlayer ][ iPokeNum ][ 0 ] = EOS;

			for ( iSkillNum = 0; iSkillNum < SKILLS_PER_POKEMON; iSkillNum++ )
			{
				g_iPlayerPokemonSkills[ iPlayer ][ iPokeNum ][ iSkillNum ] = SKILL_INVALID;
				g_iPlayerSkillPP[ iPlayer ][ iPokeNum ][ iSkillNum ] = 0;
			}
		}

		PlayerSetInfo( iPlayer, PINFO_VERSION, 0 );

		pokemod_funcend_debug( "LoadPlayer No Version" );

		return;
	}

	for ( new Int:iVersion; iVersion < PM_VERSION; iVersion++ )
	{
		if ( equali( szVersion, szPokeModVersion[iVersion] ) )
		{
			PlayerSetInfo( iPlayer, PINFO_VERSION, iVersion );
			break;
		}
	}

	PlayerSetInfo( iPlayer, PINFO_HUD_COLOR, nfv_get_num( szPlayerFile, "PokeMod", "HudColor" ) );

	PlayerSetInfo( iPlayer, PINFO_SETTING_AUTOCATCH, nfv_get_num( szPlayerFile, "PokeMod", "AutoBall" ) );
	PlayerSetInfo( iPlayer, PINFO_SETTING_SHOWDAMAGE, nfv_get_num( szPlayerFile, "PokeMod", "ShowSkillDamage" ) );
	PlayerSetInfo( iPlayer, PINFO_SETTING_XPNUM, nfv_get_num( szPlayerFile, "PokeMod", "ShowXPNum" ) );

	PlayerSetInfo( iPlayer, PINFO_HUDPOS, nfv_get_num( szPlayerFile, "PokeMod", "HudPos" ) );
	PlayerSetInfo( iPlayer, PINFO_DHUDPOS_X, nfv_get_num( szPlayerFile, "PokeMod", "DHudPosX" ) );
	PlayerSetInfo( iPlayer, PINFO_DHUDPOS_Y, nfv_get_num( szPlayerFile, "PokeMod", "DHudPosY" ) );

	new String:szLocation[ NAME_LEN ];
	nfv_get_data( szPlayerFile, "PokeMod", "Location", szLocation, charsmax( szLocation ) );
	PlayerSetInfo( iPlayer, PINFO_LOCATION, clamp( NameToLocation( szLocation ), 0, ArraySize( g_iLocationNames ) ) );

	nfv_get_data( szPlayerFile, "PokeMod", "LastPokeCenter", szLocation, charsmax( szLocation ) );
	PlayerSetInfo( iPlayer, PINFO_LAST_POKECENTER, clamp( NameToLocation( szLocation ), 0, ArraySize( g_iLocationNames ) ) );

	for ( new Int:iPokeNum; iPokeNum < POKEMON_PER_PLAYER; iPokeNum++ )
	{
		LoadPlayerPokemon( iPlayer, iPokeNum );
	}

	new String:szData[ NAME_LEN ], String:szName[ NAME_LEN ];
	new Int:iNameLine;

	while ( nfv_get_linedata( szPlayerFile, "PokeModSpecial", NFV_WILDCARD, szData, charsmax( szData ),
			.name_line_start = ( iNameLine + 1 ), .name_line_find = iNameLine,
			.name_copy = szName, .name_len = charsmax( szName )
			)
	){
		PlayerGiveSpecial( iPlayer, szName, .save = false );
	}

	iNameLine = 0;

	new Int:iAmount;
	while ( nfv_get_linedata( szPlayerFile, "PokeItems", NFV_WILDCARD, szData, charsmax( szData ),
			.name_line_start = ( iNameLine + 1 ), .name_line_find = iNameLine,
			.name_copy = szName, .name_len = charsmax( szName )
			)
	){
		if ( ( iAmount = str_to_num( szData ) ) )
			PlayerGiveItem( iPlayer, NameToItem( szName ), iAmount, .save = false );
	}

	SkillUpdateCalls();

	pokemod_funcend_debug( "LoadPlayer Loaded" );
}

PM_FUNC_NORM_ATTRIB LoadPlayerPokemon( Int:iPlayer, Int:iPokeNum )
{
	pokemod_funcstart_debug( "LoadPlayerPokemon" );

	new String:szPlayerFile[ 64 ];
	nfv_player_file( iPlayer, szPlayerFile, charsmax( szPlayerFile ) );

	new String:szPokeSlot[ NAME_LEN ], String:szSkillSlot[ NAME_LEN ], String:szSkill[ NAME_LEN ];
	SaveNameSlot( szPokeSlot, charsmax(szPokeSlot), iPokeNum );

	new Int:iTell = nfv_key_tell( szPlayerFile, szPokeSlot );

	nfv_get_data( szPlayerFile, szPokeSlot, "Name", g_szPlayerPokemonNames[ iPlayer ][ iPokeNum ], charsmax( g_szPlayerPokemonNames[][] ), .key_tell_start = iTell );
	nfv_get_data( szPlayerFile, szPokeSlot, "OriginalOwner", g_iPlayerPokemonOwner[ iPlayer ][ iPokeNum ], charsmax( g_iPlayerPokemonOwner[][] ), .key_tell_start = iTell );

	for ( new Int:iSkillNum = 0; iSkillNum < SKILLS_PER_POKEMON; iSkillNum++ )
	{
		formatex( szSkillSlot, charsmax(szSkillSlot), "Skill%dName", iSkillNum );
		nfv_get_data( szPlayerFile, szPokeSlot, szSkillSlot, szSkill, charsmax( szSkill ), .key_tell_start = iTell );
		g_iPlayerPokemonSkills[ iPlayer ][ iPokeNum ][ iSkillNum ] = NameToSkill( szSkill );

		formatex( szSkillSlot, charsmax(szSkillSlot), "Skill%dPP", iSkillNum );
		g_iPlayerSkillPP[ iPlayer ][ iPokeNum ][ iSkillNum ] = nfv_get_num( szPlayerFile, szPokeSlot, szSkillSlot, .key_tell_start = iTell );
	}

	for ( new PM_POKESTAT:iStatNum; iStatNum < PM_POKESTAT; iStatNum++ )
	{
		//Skip Pokemon ID, we will find it after this
		if ( iStatNum == POKESTAT_POKEMON )
			continue;

		g_iPlayerPokemonStats[ iPlayer ][ iPokeNum ][ iStatNum ] = nfv_get_num( szPlayerFile, szPokeSlot, gPokeStatNames[ iStatNum ], .key_tell_start = iTell );
	}

	//Check how the Pokemon ID was saved
	new String:szPokeName[ NAME_LEN ];
	nfv_get_data( szPlayerFile, szPokeSlot, gPokeStatNames[ POKESTAT_POKEMON ], szPokeName, charsmax( szPokeName ), .key_tell_start = iTell );
	new Int:iPokeID = str_to_num( szPokeName );

	//If it is an ID number, change it to a Pokemon Name, incase IDs change
	if ( iPokeID )
	{
		nfv_set_data( szPlayerFile, szPokeSlot, gPokeStatNames[ POKESTAT_POKEMON ], PokeToName( iPokeID ) );
		g_iPlayerPokemonStats[ iPlayer ][ iPokeNum ][ POKESTAT_POKEMON ] = iPokeID;
	}
	else
	{
		g_iPlayerPokemonStats[ iPlayer ][ iPokeNum ][ POKESTAT_POKEMON ] = NameToPoke( szPokeName );
	}

	pokemod_funcend_debug( "LoadPlayerPokemon" );
}

